package thread.lock;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.TimeUnit;

public class lock8 {
    public static void main(String[] args) {
        Phone phone = new Phone();
        Phone phone2 = new Phone();
        
        new Thread(()->{
            phone.sendSms();
        },"A").start();

        new Thread(()->{
            phone2.sendSms();
        },"B").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        new Thread(()->{
            phone.call();
        },"C").start();
        new Thread(()->{
            phone2.call();
        },"D").start();

        
    }
    
}

class Phone{

//    synchronized 锁的对家是方法的调用者!
//    两个方法用的是同一个锁，谁先拿到谁执行!
    public static synchronized void sendSms(){
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(Thread.currentThread().getName() + "发短信");
    }
    public static synchronized void call(){
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(Thread.currentThread().getName() + "打电话");
        
    }
    

    
}
